package com.yry.security;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;

/**
 * 3、单向加密：消息摘要算法加密——HMAC
 * HMAC(Keyed-Hash Message Authentication Code)，含有密钥的散列函数算法
 * MD系列：HmacMD2、HmacMD4、HmacMD5
 * SHA系列：HmacSHA1、HmacSHA224、HmacSHA256、HmacSHA384、HmacSHA512
 * JDK:HmacMD5、HmacSHA1、HmacSHA256、HmacSHA384、HmacSHA512
 * BC：全部
 * 
 * @author Administrator
 *
 */
public class HMACUtils {
	/**
	 * 产生MD5的密钥：通过手动输入CHAR数组产生————方法1
	 * @return
	 */
	public static String createMD5SecretKey(){
		//可以更改为随机生成指定长度的字符串与数字的组合：RandomStringUtils.randomAlphanumeric(5);
		String key = "aaaaaaaaaa";
		return key;
	}
	
	/**
	 * 产生密钥：JDK 
	 * algorithm:HmacMD5、HmacSHA1、HmacSHA256、HmacSHA384、HmacSHA512
	 * @param algorithm
	 * @return
	 */
	public static String JdkSecretKey(String algorithm){
		String key=null;
		try {
			//			初始化KeyGenerator;
			KeyGenerator keyGenerator = KeyGenerator.getInstance(algorithm);
			//			产生密钥；
			SecretKey  secretKey = keyGenerator.generateKey();
			//			获得密钥
			byte[] keyArray = secretKey.getEncoded();
			key = Hex.encodeHexString(keyArray);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}

		return key;
	} 	

	
 
	/**
	 * 消息摘要算法加密JDK:HmacMD5、HmacSHA1、HmacSHA256、HmacSHA384、HmacSHA512
	 * algorithm:HmacMD5、HmacSHA1、HmacSHA256、HmacSHA384、HmacSHA512
	 * @param algorithm
	 * @param keyStr
	 * @param srcStr
	 * @return
	 */
	public static String jdkHmacX(String algorithm,String keyStr,String srcStr){
		String hmacStr = null;
		try {			
			//设置密钥；
			byte[] key = Hex.decodeHex(keyStr.toCharArray());
						
			//还原密钥
			SecretKey restoreSecretKey = new SecretKeySpec(key,algorithm);
			//实例化MAC                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
			Mac mac = Mac.getInstance(restoreSecretKey.getAlgorithm());
			//初始化Mac
			mac.init(restoreSecretKey);
			//执行摘要；
			byte[] hmacBytes = mac.doFinal(srcStr.getBytes());
			//转成16进制字符串
			hmacStr = Hex.encodeHexString(hmacBytes);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (DecoderException e) {
			e.printStackTrace();
		}  
		return hmacStr;
	}
 
 
 	
}
